//
//  DataBaseFMDB.m
//  GoodAppetite
//
//  Created by 哲 on 15/8/17.
//  Copyright (c) 2015年 xusizhe. All rights reserved.
//

#import "DataBaseFMDB.h"
#import "MXFindDBModel.h"
#import "MXDataBaseItemModel.h"
@implementation DataBaseFMDB
- (void)dealloc
{
    [_arrayBig release];
    [super dealloc];
}   
-(void)createTopicDataBase
{
//    NSLog(@" ========== >>>>>>%@", NSHomeDirectory());
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSString *dbPath = [documentPath stringByAppendingPathComponent:@"topicTwoDaseData.sqlite"];
    db = [FMDatabase databaseWithPath:dbPath];
    
}

- (void)creatTopicDaseTable
{
    if ([db open]) {
        NSString *sqlCreate = [NSString stringWithFormat:@"create table if not exists topicData (id integer primary key autoincrement, name text, AlbumID text, TypeID text,imagePath text)"];
        BOOL result = [db executeUpdate:sqlCreate];
        if (result) {
            NSLog(@"建表成功");
        } else {
            NSLog(@"建表失败");
        }
//                [db close];
    }
}

- (BOOL)insertTopicTable :(NSString *)topicName AlbumID:(NSString *)AlbumID TypeID:(NSString *)TypeID imagePath:(NSString *)imagePath
{
   
    if ([db open]) {
        NSString *sqlInsert = [NSString stringWithFormat:@"insert into topicData (name, albumID, typeID ,imagePath) values ('%@', '%@','%@', '%@')", topicName, AlbumID, TypeID,imagePath];
        BOOL result = [db executeUpdate:sqlInsert];
        if (result) {
                        [db close];
            return YES;
        } else {
                        [db close];
            return NO;
        }
    }
        [db close];
    return NO;
}


- (BOOL)isTopicExists:(NSString *)actuvity
{
    if ([db open]) {
        NSString *sqlSelect = [NSString stringWithFormat:@"select * from topicData"];
        FMResultSet *set = [db executeQuery:sqlSelect];
        while ([set next]) {
            NSString *str = [set stringForColumn:@"name"];
            if ([actuvity isEqualToString:str]) {
                [db close];
                return YES;
            }
        }
    }
        [db close];
    return NO;
}


-(void)deleteTopicData:(NSString *)name
{
    if ([db open]) {
        NSString *sqlDelete =[NSString stringWithFormat:@"delete from topicData where name ='%@'",name];
        BOOL result=[db executeUpdate:sqlDelete];
        if (result) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }
    }
    [db close];
    
}

-(NSMutableArray *)selectTopicData
{
    self.arrayBig = [NSMutableArray array];
    if ([db open]) {
        // NSString *sqlSelect =[NSString stringWithFormat:@"select * from student"];
        //集合中存储了表中所有内容
        NSString *sqlSelect =[NSString stringWithFormat:@"select * from topicData" ];
        FMResultSet *set =[db executeQuery:sqlSelect];
        while ([set next]) {
            //接收
            NSString *name = [set stringForColumn:@"name"];
            NSString *AlbumID =[set stringForColumn:@"AlbumID"];
            NSString *TypeID = [set stringForColumn:@"TypeID"];
            NSString *imagePath =[set stringForColumn:@"imagePath"];
            
            MXFindDBModel *model =[MXFindDBModel getTopicModel];
            model.detail = name;
            model.AlbumID = AlbumID;
            model.TypeID = TypeID;
            model.imagePath =imagePath;
            [self.arrayBig addObject:model];
        }
        if (set) {
            NSLog(@"查询成功");
        }else {
            NSLog(@"查询失败");
        }
        
//                [db close];
    }
    return _arrayBig;
}

//========================================== 商品数据部分 ===================================

-(void)createItemDataBase
{
    NSLog(@"%@", NSHomeDirectory());
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSString *dbPath = [documentPath stringByAppendingPathComponent:@"itemTwoDaseData.sqlite"];
    db = [FMDatabase databaseWithPath:dbPath];
    
}


- (void)creatItemDaseTable
{
    if ([db open]) {
        NSString *sqlCreate = [NSString stringWithFormat:@"create table if not exists itemData (id integer primary key autoincrement, name text, ItemID text,imagePath text, price text)"];
        BOOL result = [db executeUpdate:sqlCreate];
        if (result) {
            NSLog(@"建表成功");
        } else {
            NSLog(@"建表失败");
        }
        //                [db close];
    }
}

- (BOOL)insertItemTable :(NSString *)itemName ItemID:(NSString *)ItemID imagePath:(NSString *)imagePath Price:(NSString *)price
{
    if ([db open]) {
        NSString *sqlInsert = [NSString stringWithFormat:@"insert into itemData (name, ItemID ,imagePath, price) values ('%@', '%@','%@', '%@')", itemName, ItemID,imagePath, price];
        BOOL result = [db executeUpdate:sqlInsert];
        if (result) {
            [db close];
            return YES;
        } else {
            [db close];
            return NO;
        }
    }
    [db close];
    return NO;
    
}

- (BOOL)isItemExists:(NSString *)actuvity
{
    if ([db open]) {
        NSString *sqlSelect = [NSString stringWithFormat:@"select * from itemData"];
        FMResultSet *set = [db executeQuery:sqlSelect];
        while ([set next]) {
            NSString *str = [set stringForColumn:@"name"];
            if ([actuvity isEqualToString:str]) {
                [db close];
                return YES;
            }
        }
    }
    [db close];
    return NO;
}

-(void)deleteItemData:(NSString *)name
{
    if ([db open]) {
        NSString *sqlDelete =[NSString stringWithFormat:@"delete from itemData where name ='%@'",name];
        BOOL result=[db executeUpdate:sqlDelete];
        if (result) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }
    }
    [db close];
}

-(NSMutableArray *)selectItemData
{
    self.arrayBig = [NSMutableArray array];
    if ([db open]) {
        //集合中存储了表中所有内容
        NSString *sqlSelect =[NSString stringWithFormat:@"select * from itemData" ];
        FMResultSet *set =[db executeQuery:sqlSelect];
        while ([set next]) {
            //接收
            NSString *name = [set stringForColumn:@"name"];
            NSString *ItemID =[set stringForColumn:@"ItemID"];
            NSString *imagePath =[set stringForColumn:@"imagePath"];
            NSString *price = [set stringForColumn:@"price"];
            NSInteger number = [set intForColumn:@"id"];
            
            MXDataBaseItemModel *model =[MXDataBaseItemModel getItemModel];
            model.detail =name;
            model.ItemID =ItemID;
            model.imagePath =imagePath;
            model.price = price;
            model.number = number;
            
            [self.arrayBig addObject:model];
            
        }
        if (set) {
            //            NSLog(@"查询成功");
        }else {
            //            NSLog(@"查询失败");
        }
        
        [db close];
    }
    return _arrayBig;
}

@end
